package com.snatch.gather.system;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.snatch.common.utils.PropsLoader;
import com.snatch.common.vo.NodeWeb;
import com.snatch.common.vo.WebConfig;
import com.snatch.gather.constant.SystemConstant;
import com.snatch.gather.service.AccountService;
import com.snatch.gather.util.XmlConfigUtil;
import com.snatch.gather.vo.SnatchConfig;
import com.snatch.gather.vo.SnatchGame;
import com.snatch.gather.vo.SnatchItem;
import com.snatch.gather.vo.SnatchWeb;


/**
 * @author ltj 节点管理系统初始化类
 */
@Component
public class SystemMgr {
	private static Logger logger = Logger.getLogger(SystemMgr.class.getName());
	@Autowired
	private PropsLoader propsLoader;
	
	@Autowired
	AccountService accountManageService;
	
	private static final boolean TEST = false;
	
	/**
	 * 初始化的数据
	 */
	@PostConstruct
	public void init() {
		initCfgs();
		if(!TEST){
			initAccount();
			initSnatch();
		}
		
		/*initDatas();
		initJms();
		initDaemon();*/
	}

	/**
	 * 销毁系统级别的资源
	 */
	@PreDestroy
	public void destroy() {
		/*// 销毁ICE客户端
		nodeManageIntegration.destoryAllIceClient();
		// 销毁ICE服务器
		IceServerUtil.destory();*/
	}

	/**
	 * 初始化的守护线程
	 */
	private void initDaemon() {
		/*clearDataDaemon.start();// 清理过期数据后台线程
		logger.info("清理过期数据线程线程启动成功");

		removeNodeDaemon.start();// 移除问题抓取节点后台线程
		logger.info("移除问题抓取节点后台线程线程启动成功");

		sendGQThirdRatioDaemon.start();
		sendSPThirdRatioDaemon.start();
		sendBFThirdRatioDaemon.start();
		
		sendGQAutoSysRatioDaemon.start();// 启动第三方赔率发送给铺货线程
		sendSPAutoSysRatioDaemon.start();

		findTradeItemIdDaemon.start();// 主动去数据中心获取交易项id
		
		sendDataDaemon.start();*/
		logger.info("主动去数据中心获取交易项id线程启动成功");

	}

	
	
	/**
	 * 初始化的系统配置
	 */
	private void initCfgs() {
		FileInputStream fileInputStream = null;
		try {
			ClassLoader loader = SystemMgr.class.getClassLoader();
			URL url = loader.getResource(SystemConstant.ACCOUNT_CONFIG);
			fileInputStream = new FileInputStream(new File(url.getFile()));
			NodeWeb nodeWeb = XmlConfigUtil.xmlChangeNodeWeb(fileInputStream);
			SystemData.nodeWebs = nodeWeb;
			
			
			url = loader.getResource(SystemConstant.SNATCH_CONFIG);
			fileInputStream = new FileInputStream(new File(url.getFile()));
			SnatchConfig snatchConfig = XmlConfigUtil.xmlChangeSnatchConfig(fileInputStream);
			
			for(SnatchWeb snatchWeb : SystemData.snatchConfig.getSnatchWebs()){
				for(SnatchGame snatchGame : snatchWeb.getSnatchGames()){
					for(SnatchItem snatchItem : snatchGame.getSnatchItems()){
						snatchItem.setSnatchGame(snatchGame);
						snatchItem.setSnatchWeb(snatchWeb);
					}
				}
			}
			SystemData.snatchConfig = snatchConfig;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fileInputStream!=null){
				try {
					fileInputStream.close();
					fileInputStream = null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public void initAccount(){
		for(WebConfig webConfig : SystemData.nodeWebs.getWebConfigs()){
			if(webConfig.isAuto()){
				accountManageService.loginAccount(webConfig.getWebCode());
			}
		}
	}
	
	public void initSnatch(){
		for(SnatchWeb snatchWeb : SystemData.snatchConfig.getSnatchWebs()){
			for(SnatchGame snatchGame : snatchWeb.getSnatchGames()){
				for(SnatchItem snatchItem : snatchGame.getSnatchItems()){
					
				}
			}
		}
	}
}
